package org.concepts.algorithm.sort;

import java.util.ArrayList;
import java.util.List;

import org.junit.Test;

public class TestSort {

  @Test
  public void testSelectionSort() {

    for (Number[]  data : getInputData()) {
      SelectionSort<Number> sorter = new SelectionSort<Number>();
      sorter.setUseRecursion(false);
      System.out.println("recursion: " + sorter.isUseRecursion());
      sort(sorter,data);
    }
    
    for (Number[]  data : getInputData()) {
      SelectionSort<Number> sorter = new SelectionSort<Number>();
      sorter.setUseRecursion(true);
      System.out.println("recursion: " + sorter.isUseRecursion());
      sort(sorter,data);
    }
  }

  @Test
  public void testBubbleSort() {
    for (Number[]  data : getInputData()) {
      BubbleSort<Number> sorter = new BubbleSort<Number>();
      sort(sorter,data);
    }
  }
  

  private List<Number[]> getInputData() {
    List<Number[]> inputData = new ArrayList<Number[]>();
    
    Integer[] empty = {};
    inputData.add(empty);
    Integer[] one = { 1 };
    inputData.add(one);
    Float[] oneF = { 1.1f };
    inputData.add(oneF);
    Integer[] two = { 5, 1 };
    inputData.add(two);
    Float[] twoF = { 3.1f, 1.1f };
    inputData.add(twoF);
    Integer[] firstTen = { 4, 6, 8, 1, 2, 9, 3, 5, 7, 0 };
    inputData.add(firstTen);
    
    return inputData;
  }
  
  private void sort(Sorter sorter, Number[] input) {
    print(input, null);
    print(null, sorter.sort(input));
  }

  private void print(Number[] input, Number[] output) {
    StringBuilder data = new StringBuilder();

    if (input != null) {
      data.append(" input: ");
      for (int intCnt = 0; intCnt < input.length; intCnt++) {
        data.append(input[intCnt] + ",");
      }
    }

    if (output != null) {
      data.append(" output: ");
      for (int outCnt = 0; outCnt < output.length; outCnt++) {
        data.append(output[outCnt] + ",");
      }
    }

    System.out.println(data);
  }

}
